/**
 * Created by admin on 2016/8/12.
 */
var cart = function () {

    /*删除操作*/
    var deleteGood = function () {
        /*删除时需要进行确认*/
        $('.delete-icon').on('click', function () {
            $('#delete').modal({
                relatedTarget: this,
                onConfirm: function (options) {
                },
                // closeOnConfirm: false,
                onCancel: function () {
                }
            });
        });
    };

    /*加减数量操作*/
    var goodCount = function () {
        var plus = $('.plus-icon');
        plus.on('click', function () {
            var self = $(this);
            var para = self.parents('.good-box');
            var price = parseFloat(para.find('span.money-font').text().substr(1));
            var i = para.find('.am-u-sm-1 i');
            /*进行操作前对是否处于选中状态进行判断*/
            if (i.hasClass('unchecked-icon')) {
                /*没有选中，正常加*/
                handle();
            } else {
                /*选中，在正常加的同时，对底部总金额进行变动*/
                handle();
                var totalMoney = $('.totalMoney');
                var total = parseFloat(totalMoney.text().substr(1));
                total += price;
                totalMoney.text("￥" + total);
            }
            function handle() {
                var count = parseInt(self.siblings('input').val());
                var curCount = parseInt(count) + 1;
                self.siblings('input').val(curCount);
            }
        });


        var cut = $('.cut-icon');
        cut.on('click', function () {
            var self = $(this);
            var para = self.parents('.good-box');
            var count = parseInt(self.siblings('input').val());
            var i = para.find('.am-u-sm-1 i');
            /*进行操作前对是否处于选中状态进行判断*/
            if (i.hasClass('unchecked-icon')) {
                /*没有选中，正常减*/
                var curCount = count - 1;
                self.siblings('input').val(curCount);
            } else {
                /*选中，在正常减的同时，对底部总金额进行变动*/
                var price = parseFloat(para.find('span.money-font').text().substr(1));
                var totalMoney = $('.totalMoney');
                var total = parseFloat(totalMoney.text().substr(1));
                if (count == 1) {
                    alert("不能再少啦");
                    totalMoney.text("￥" + total);
                    return false;
                } else {
                    var curCount2 = count - 1;
                    self.siblings('input').val(curCount2);
                    total -= price;
                    totalMoney.text("￥" + total);
                }
            }
        });
    };


    /*全选*/
    var selectAll = function () {
        $('#selAll').click(function () {
            var i = $(this).find('i');
            var prices = $('.good-box').find('span.money-font');
            var numbers = $('.good-box').find('input');
            var sum = 0;
            for (var j = 0; j < prices.length; j++) {
                console.log(prices[j]);
                sum += parseFloat(prices.eq(j).text().substr(1)) * parseInt(numbers.eq(j).val());
            }
            if (i.hasClass('unchecked-icon')) {
                /*改变本身的样式*/
                i.removeClass('unchecked-icon').addClass('checked-icon');
                /*改变购物车的选中样式*/
                $('.good-box').find('.am-u-sm-1 i').removeClass('unchecked-icon').addClass('checked-icon');
                /*改变金额*/
                $(' #buyBox span.money-font').text("￥" + sum);
            } else {
                i.addClass('unchecked-icon').addClass('checked-icon');
                $('.good-box').find('.am-u-sm-1 i').addClass('unchecked-icon').removeClass('checked-icon');
                $('#buyBox span.money-font').text("￥" + 0);
            }
        })
    };

    /*单选结算*/
    var singlePay = function () {
        var i = $('.good-box').find('.am-u-sm-1 i');
        var length = i.length;
        var turn = 0;
        i.on('click', function () {
            var self = $(this);
            var para = self.parent().parent('.good-box');
            var money = para.find('span.money-font').text().substr(1) * (para.find('input').val());
            var totalMoney = parseFloat($('.totalMoney').text().substr(1));
            if (self.hasClass('unchecked-icon')) {
                turn++;
                self.removeClass('unchecked-icon').addClass('checked-icon');
                totalMoney = totalMoney + money;
                $('.totalMoney').text("￥" + totalMoney);
            } else {
                turn--;
                console.log(turn);
                self.removeClass('checked-icon').addClass('unchecked-icon');
                totalMoney = parseFloat(totalMoney - money).toFixed(1);
                $('.totalMoney').text("￥" + totalMoney);
            }
            if(turn == length){
                $('#selAll').find( 'i').removeClass('unchecked-icon').addClass('checked-icon');
            }else{
                $('#selAll').find( 'i').addClass('unchecked-icon').removeClass('checked-icon');
            }
        })
    };


    return {
        init: function () {
            deleteGood();
            goodCount();
            selectAll();
            singlePay();
        }
    }
}();
